<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8" session="false" %>
<%@ page import="com.atozsoft.commonutil.Constants"%>
<%@ page import="java.util.Date"%>
<%@ page import="java.text.SimpleDateFormat"%>
	
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- 
  - Author(s): houfy
  - Date: 2014-10-31 14:12:31
  - Description:
-->
<head>
    <%@include file="/common.jsp"%>
	<%@include file="/common/common.jsp"%>
    <%@include file="/coframe/tools/skins/common.jsp" %>
	<title>流程明细框架</title>
    <%
 	 	//流程实例ID
 	 	String processInstId = "";
		if(request.getParameter("processInstId")!=null){
			processInstId = (String)request.getParameter("processInstId");
		}
		
		String workitemid = "";	
		if(request.getParameter("workitemid")!=null){
			workitemid = (String)request.getParameter("workitemid");
		}
		
		String webType = "";
		if(request.getParameter("webType")!=null){
			webType = (String)request.getParameter("webType");
		}
		
		String fromWeb = ""; //判断页面来源
		if(request.getParameter("fromWeb")!=null){
			fromWeb = (String)request.getParameter("fromWeb");
		}
		
		// 流程绑定的id
    	String keyvalue = "-1";
    	keyvalue = (String)request.getParameter("keyvalue");
		
 	%>
</head>
<body>
	<div id="processBtn" class="nui-toolbar" style="width:100%;padding:0px;" borderStyle="border:0;">
		<table style="width:100%;">
	    	<tr style="width:100%;">
	        	<td style="text-align:right;">
	          		<a id="saveBtn"              class="nui-button" iconCls="icon-save"    onclick="onSave()">保存</a>
	          		<a id="cancelBtn"            class="nui-button" iconCls="icon-cancel"  onclick="onCancel()">退回</a>
	          		<a id="rollbackBtn"          class="nui-button" iconCls="icon-upgrade" onclick="onRollback()">撤回</a>
	          		<a id="stopBtn"              class="nui-button" iconCls="icon-split"    onclick="onStop()">废标</a>
	          		<a id="tranBtn"              class="nui-button" iconCls="icon-lock"    onclick="onTransfer()">移交</a>
	          		<a id="okBtn"                class="nui-button" iconCls="icon-ok"      onclick="self['infoFrame'].onParentCallBeforeOk()">提交</a>
	        	</td>
	      	</tr>
	    </table>
	</div>
	<div class="nui-fit">
		<div id="tabs1" class="nui-tabs" activeIndex="0" style="width:100%;height:100%;" plain="true" onactivechanged="changeTab">
			<div id="formTab" name="formTab" title="表单" style="width:100%;height:100%;"url="<%=request.getContextPath()%>/mis/projectEstablishment/projectEstablishment_detail.jsp?actionFlg=process"></div>
			<div id="approveTab" name="approveTab"  title="审批意见" url="<%=request.getContextPath()%>/wf/processOperate/process_Detail_approve.jsp?processInstId=<%=processInstId%>"></div>
			<div id="processTab" name="processTab"  title="流程" url="<%=request.getContextPath()%>/wf/common/showWFGraphbyDefId.jsp?processID=1" style="width:100%;height:100%;"></div>
		</div>  
	</div>
	<script type="text/javascript">
    	nui.parse();
    	
    	var strUrl = "";
    	//获取工作项URL，填充到iframe的src中
        getWorkItemUrl();
    	var tabs = nui.get("tabs1");
    	var formTab= tabs.getTab("formTab");
    	var approveTab= tabs.getTab("approveTab");
    	var processTab= tabs.getTab("processTab");

    	
    	//页面操作按钮加载(加载状态分：阅读：read；操作：operate；已完成：done)
    	var webType = "<%=webType %>";
    	//获取页面来源
    	var fromWeb = "<%=fromWeb%>"
    	var workitemid = "<%=workitemid %>";
    	var processInstId="<%=processInstId%>";
    	var keyvalue = "<%=keyvalue %>";
    	//获取当前流程实例的实际工作项
    	//判断当前操作是否合法
		showProcessWorkItemBePlay();
    	
    	if(webType!=null && webType!=""){
    		//已完成待办进入后取得读取操作
    		if(webType=="operate"){ //待办
    			hideAll();
    			showButton();
    			//流程审批操作时，不可执行撤回操作
    			$("#rollbackBtn").hide();
    		}else if(webType=="done"){ //已完成待办
    			hideAll();
    			showButton();
    			$("#okBtn").hide();
    			$("#cancelBtn").hide();
    			$("#saveBtn").hide();
    			$("#stopBtn").hide();
    			$("#tranBtn").hide();
    		}
    	}
    	
    	//获取当前流程实例的状态
        //流程已经完成或终止，所有操作不可用
        getProcessInstState();
    	
    	//隐藏全部按钮
    	function hideAll(){
    		$("#cancelBtn").hide();
			$("#okBtn").hide();
			$("#rollbackBtn").hide();
			$("#stopBtn").hide();
    		$("#saveBtn").hide();
    		$("#virCancelBtn").hide();
    		$("#tranBtn").hide();
    		nui.layout();
    	}
    	
    	//新增TAB
    	var index = 4;
    	function addTab(taburl,tabname) {
            var tabs = nui.get("tabs1");
            //add tab
            var tab = { title: tabname, url: taburl, showCloseButton: true };
            //tab.url = taburl; 
            tab = tabs.addTab(tab);            
            //tab body
            var el = tabs.getTabBodyEl(tab);
            el.innerHTML = index;
            //active tab
            tabs.activeTab(tab);
        }
        
    	//查询工作项对应的按钮
    	function showButton(){
    		var buttonlist = "2,3,5,6";
            var buttonarray = buttonlist.split(",");
            for (var i=0 ; i< buttonarray.length ; i++)
			{
				var tempbuttonarray = buttonarray[i].split("-");
				if(tempbuttonarray[0]=="1"){  //1退回
					nui.get("cancelBtn").setText(tempbuttonarray[1]);
					$("#cancelBtn").show();
				}else if(tempbuttonarray[0]=="2"){ //2撤回
					nui.get("rollbackBtn").setText(tempbuttonarray[1]);
					$("#rollbackBtn").show();
				}else if(tempbuttonarray[0]=="3"){ //3终止
					nui.get("stopBtn").setText(tempbuttonarray[1]);
					$("#stopBtn").show();
				}else if(tempbuttonarray[0]=="4"){ //4同意
					nui.get("okBtn").setText(tempbuttonarray[1]);
					$("#okBtn").show();
				}else if(tempbuttonarray[0]=="5"){ //5保存
					nui.get("saveBtn").setText(tempbuttonarray[1]);
					$("#saveBtn").show(); 
					saveStr = "true";
				}else if(tempbuttonarray[0]=="6"){ //5保存
					nui.get("tranBtn").setText(tempbuttonarray[1]);
					$("#tranBtn").show(); 
				}
				
			}
    	}
    	
    	// 切换tab时重新加载
	    function changeTab(e){
			var tabs = nui.get("tabs1");
			var tab = tabs.getActiveTab();
	    	if(tab.title=="审批意见"){
		    	var strUrl = "process_Detail_approve.jsp?processInstId=<%=processInstId%>";
				tabs.loadTab(strUrl, approveTab);
				//重载Tab，显示页面
				tabs.reloadTab(approveTab);
			}
		}
    	
    	//获取当前流程实例的状态
		function getProcessInstState(){
			var processInstId = "<%=processInstId %>";
        	var json = nui.encode({processInstId:processInstId});
	        $.ajax({
	            url: "com.atozsoft.wf.processOperate.queryProcessInstState.biz.ext",
	            type: 'POST',
	            data:json,
	            cache: false,
	            async: false,
	            contentType: 'text/json',
	            success: function(text) {
	                var currentState = text.currentState;
	                if(currentState == '7' || currentState == '8'){
	                	//流程已经完成或终止，所有操作不可用
	                	hideAll();
	                }
	          }
	        });
		}
    	
    	//获取当前流程实例的实际工作项
		function showProcessWorkItemBePlay(){
			var processInstId = "<%=processInstId %>";
			var workitemid = "<%=workitemid %>";
        	var json = nui.encode({processInstId:processInstId});
	        $.ajax({
	            url: "com.atozsoft.wf.processOperate.queryProcessWorkItemBeplayByPid.biz.ext",
	            type: 'POST',
	            data:json,
	            cache: false,
	            async: false,
	            contentType: 'text/json',
	            success: function(text) {
	                if(text=="" || text == null){
		               hideAll();
	                }else{
	                	var data = text.datas[0];
		                if(data != null && data != ""){
			                if(data.workItemID != workitemid){
			                	webType = "done";
			                }
		                }
	                }
	          }
	        });
		}
		
        //获取工作项URL
        function getWorkItemUrl(){
        	var workitemid = "<%=workitemid %>";
        	var json =nui.encode({workitemid:workitemid});
	        $.ajax({
	            url: "com.atozsoft.wf.processOperate.queryWorkItemUrl.biz.ext",
	            type: 'POST',
	            data:json,
	            cache: false,
	            async: false,
	            contentType: 'text/json',
	            success: function(text) {
	                strUrl = text.strUrl;
	          }
	        });
        }
    	
    	
    	//退回
	    function onCancel(){
	   		var btnEdit = this;
	   		// 调用子界面方法判断是否指定退回节点
	   		var activityDefID = self["infoFrame"].onParentCallBeforeCancel();
	   		// 否则默认：回退前一个点
	        if(activityDefID == "") {
	   			// 根据流程ID获取上一个节点的activityDefID
	        	json = nui.encode({workitemid:workitemid});
		        $.ajax({
		            url: "com.atozsoft.wf.processOperate.queryPreActivityDefID.biz.ext",
		            type: 'POST',
		            data:json,
		            cache: false,
		            async: false,
		            contentType: 'text/json',
		            success: function(text) {
		                activityDefID = text.activityDefID;
		            }
		        });
	        } else if(activityDefID == "false") {
	        	return false;
	        }else if(activityDefID == "selectSpecialBack"){
	        	var json =nui.encode({processInstId:processInstId});
				$.ajax({
		    		url: "com.atozsoft.wf.processOperate.quertyProcessPass4.biz.ext",
		    		type: 'POST',
		    		data: json,
		    		cache: false,
		    		async:false,
		    		contentType:'text/json',
		    		success: function (text) {
		    			if(text.isExist == 'YES'){
							rollbackStatus = 4;
				    		activityDefID = "special_04";
		    			}else{
		    				rollbackStatus = 1;
				    		activityDefID = "special_01";	
		    			}
		    		}
		  		});
	        }else if(activityDefID == "selectInternalBack"){
	        	var json =nui.encode({processInstId:processInstId});
				$.ajax({
		    		url: "com.atozsoft.wf.processOperate.quertyProcessPass4.biz.ext",
		    		type: 'POST',
		    		data: json,
		    		cache: false,
		    		async:false,
		    		contentType:'text/json',
		    		success: function (text) {
		    			if(text.isExist == 'YES'){
							rollbackStatus = 4;
				    		activityDefID = "internal_04";
		    			}else{
		    				rollbackStatus = 1;
				    		activityDefID = "internal_01";	
		    			}
		    		}
		  		});
	        }
	   		
	    	nui.open({
	            url: "<%=request.getContextPath()%>/wf/popup/process_Operate.jsp?operateType=returnstepHandle&processInstId=<%=processInstId %>&workitemid=<%=workitemid %>&returnworkitemid="+activityDefID,
		        title: "退回",
				width: 620, 
		        height: 180,
		        allowResize:false,
		        onload: function() {
		        },
		        ondestroy: function (action) {
	                if (action == "ok") {
	                	//页面按钮控制
	                	hideAll();
	                	
	                	var status = self["infoFrame"].rollbackStatus;
				        var tableName = self["infoFrame"].tableName;
				        var t = "";
				        // 修改状态
			        	json = nui.encode({tableName:tableName,processinstId:processInstId,processStatus:status,keyPrimary:t,keyValue:t});
				        $.ajax({
				            url: "com.atozsoft.wf.processOperate.updateProcessStatus.biz.ext",
				            type: 'POST',
				            data:json,
				            cache: false,
				            async: false,
				            contentType: 'text/json',
				            success: function(text) {
				                activityDefID = text.activityDefID;
				            }
				        });
	                	
	                	nui.alert("退回成功！", "提示", function(action){
							if(action == "ok" || action == "close"){
								tabs.reloadTab(formTab);
								tabs.reloadTab(approveTab);
								tabs.reloadTab(processTab);
								self["infoFrame"].onParentCallAfterCancel();
							}
						});
						
	                	if(fromWeb!="" && fromWeb=="processlist"){
		                	window.parent.refreshTab("myProcess_List");
		                }
		                // 刷新首页
					    window.parent.parent.refreshTab("homePage");
	                }
	             }
		    });
		}
		
		//撤回
	    function onRollback(){
	   		var btnEdit = this;
	    	nui.open({
	            url: "<%=request.getContextPath()%>/wf/popup/process_Operate.jsp?operateType=rollbackHandle&processInstId=<%=processInstId %>&workitemid=<%=workitemid %>",
		        title: "撤回",
				width: 620, 
		        height: 213,
		        allowResize:false,
		        onload: function() {
		        },
		        ondestroy: function (action) {
	                if (action == "ok") {
			            window.location.href="<%=request.getContextPath()%>/wf/processOperate/process_Detail.jsp?webType=operate&workitemid=<%=workitemid %>&processInstId=<%=processInstId %>";
	                }
	                
	                if(fromWeb!="" && fromWeb=="processlist"){
	                	window.parent.refreshTab("myProcess_List");
	                }
	                window.parent.parent.refreshTab("homePage");
	             }
		    });
		}
		
		//终止
	    function onStop(){
	   		var btnEdit = this;
	    	nui.open({
	            url: "<%=request.getContextPath()%>/wf/popup/process_Operate.jsp?operateType=stopHandle&processInstId=<%=processInstId %>&workitemid=<%=workitemid %>",
		        title: "废标",
				width: 620, 
		        height: 208,
		        allowResize:false,
		        onload: function() {
		        },
		        ondestroy: function (action) {
	                if (action == "ok") {
	                	nui.alert("废标成功！", "提示", function(action){
		                	//页面按钮控制
				            hideAll();
				            tabs.reloadTab(formTab);
							tabs.reloadTab(approveTab);
							tabs.reloadTab(processTab);
							self["infoFrame"].onParentCallAfterStop();
						});
	                }
	                
	                if(fromWeb!="" && fromWeb=="processlist"){
	                	window.parent.refreshTab("myProcess_List");
	                }
	                window.parent.parent.refreshTab("homePage");
	             }
		    });
		}
		
		//移交
	    function onTransfer(){
	   		var btnEdit = this;
		    strUrl = "com.atozsoft.wf.processOperate.updateWorkItemParti.biz.ext";
      		nui.open({
                url: "<%=request.getContextPath()%>/mis/orgUser/selectEmployee.jsp?roleId="+'zbb',
                title: "选择移交人",
                width: 750,
                height: 540,
                onload: function () {
                	var iframe = this.getIFrameEl();
                	iframe.contentWindow.SetData({multiSelect:'false'});
                },
                ondestroy: function (action) {
          	 		if(action=='ok'){
          	 			var iframe = this.getIFrameEl();
                        var data = iframe.contentWindow.GetData();
                        data = nui.clone(data);    //必须克隆返回值
	                 	var JSON = nui.encode({empid:data.empid,empname:data.username,workitemid:<%=workitemid %>});
			      		$.ajax({
			                url: strUrl,
			                data: JSON,
			                type: "post",
			                cache: false,
			                contentType:'text/json',
			                beforeSend:loading(),
			                success: function (text) {
			                	if(text.resultStr=="SUCCESS") {
			                		endLoading();
				                	nui.alert("移交成功！", "提示", function(action){
					                	//页面按钮控制
							            hideAll();
							            tabs.reloadTab(formTab);
										tabs.reloadTab(approveTab);
										tabs.reloadTab(processTab);
										self["infoFrame"].onParentCallAfterStop();
									});
								}
					      	}
					     });
                    }
                }
            });
		}
		
    	//同意
	    function onOk(){
	   		// 获取业务画面的function
			// 根据名称获取tab
			var rTab = tabs.getTab("formTab");
			// 获取tab的Html Iframe
			var elm = tabs.getTabIFrameEl(rTab);
			// 预算总额
			var budget = self["infoFrame"].budget;
			var urlStr = "";
			var height = 213;
			
	   		if(elm != undefined){
				if(elm.contentWindow != undefined){
					var retStr = elm.contentWindow.onParentCallCheck();
					if(budget == "" || budget == null){
						urlStr = "<%=request.getContextPath()%>/wf/popup/process_Operate.jsp?operateType=agreeHandle&processInstId=<%=processInstId %>&workitemid=<%=workitemid %>&endStr=" + retStr;
					}else{
						urlStr = "<%=request.getContextPath()%>/wf/popup/process_Operate.jsp?operateType=agreeHandle&processInstId=<%=processInstId %>&workitemid=<%=workitemid %>&endStr=" + retStr+"&budget="+budget;
					}
					// 最后节点弹窗高度控制
					if(retStr == "end") {
						height = 180;
					}
					var btnEdit = this;
			    	nui.open({
			            url: urlStr,
			            title: "同意",
						width: 620, 
				        height: height,
				        allowResize:false,
				        onload: function () {
		                },
		                ondestroy: function (action) {
		                   if (action == "ok") {
			                  //页面按钮控制
			                  hideAll();
			                  nui.alert("审批已完成！", "提示", function(action){
								  if(action == "ok" || action == "close"){
										tabs.reloadTab(approveTab);
										tabs.reloadTab(processTab);
					                	//调用子页面的节点控制函数
										self["infoFrame"].onParentCallAfterOk();
								  }
							  });
			               }
			               
			               if(fromWeb!="" && fromWeb=="processlist"){
			                	window.parent.refreshTab("myProcess_List");
			               }
		                }
				    });
	   				// 刷新首页
					window.parent.parent.refreshTab("homePage");
	   			}
	   		}
		}
		
		//保存-调用业务画面操作
	    function onSave(){
	   		// 获取业务画面的function
			// 根据名称获取tab
			var rTab = tabs.getTab("formTab");
			// 获取tab的Html Iframe
			var elm = tabs.getTabIFrameEl(rTab);
	   		if(elm != undefined){
				if(elm.contentWindow != undefined){
					// 调用页面保存
					var resultStr = elm.contentWindow.onParentCallOk();
					if(resultStr == "true"){
						// 调用页面控制按钮
						elm.contentWindow.noEdit();
					} 
				}
			}	
		}
		
		//业务画面调用父页面刷新操作
	    function onBusinessCallRefresh(){
			//刷新tab页
			tabs.reloadTab(formTab);
		}
		//等待提示
		function loading(){nui.mask({el: document.body,cls: 'mini-mask-loading',html: '处理中...'});}
	    function endLoading(){nui.unmask(document.body);}
    </script>
</body>
</html>